// pages/user/invite/card/card.js
import * as zrender from '../zrender/zrender';
import * as zrhelper from '../zrender/zrender-helper';
import config from '../../../../config.js';
import {
  obj2Param
} from '../../../../utils/util';

let zr = {};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    width: 295,
    height: 504,
    avatarTemp: '',
    miniCodeTemp: '',
    avatarUrl: '',
    nickName: '',
    url: 'pages/login/accredit/accredit',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      avatarUrl,
      nickName,
    } = getApp().globalData.userInfo;
    this.data.avatarUrl = avatarUrl;
    this.data.nickName = nickName;
    const multiple = getApp().globalData.systemInfo.rpx2px;
    this.setData({
      width: 295 * multiple * 2,
      height: 504 * multiple * 2
    })

    this.getReommendAvator();
  },


  /**
   * 推荐人头像
   */
  getReommendAvator() {
    wx.showLoading({
      title: '生成中,请稍候',
      mask: true,
    });
    new Promise((resolve, rejet) => {
      wx.getImageInfo({
        src: this.data.avatarUrl,
        success: (res) => {
          resolve(res);
        },
        fail: (err) => {
          // console.log(err)
        }
      })
    }).then((res) => {
      this.data.avatarTemp = res.path;
      this.getMinicode();

      // 临时绘画
      // this.renderCanvas();
    })
  },

  /**
   * 小程序码-应聘端的
   */
  getMinicode() {
    let param = {
      scene: encodeURIComponent('memberId=' + getApp().globalData.memberId),
      page: this.data.url,
      width: 127
    }
    wx.downloadFile({
      url: config.api + 'member_invitation_record/card?' + obj2Param(param),
      success: (res) => {
        console.log(res)
        wx.getImageInfo({
          src: res.tempFilePath,
          success: (_res) => {
            console.log(_res.path)
            this.data.miniCodeTemp = _res.path;
            // 绘画
            this.renderCanvas();
          },
          fail: (err) => {
            console.log(err)
            wx.hideLoading();
            wx.showToast({
              title: err.errMsg,
              icon: 'none'
            })
          }
        })
      }
    })
  },

  zrAddImage(x, y, url, w, h) {
    zr.add(new zrender.Image({
      style: {
        x: x,
        y: y,
        image: url,
        width: w,
        height: h
      }
    }));
  },

  zrAddText(x, y, text, textFont, textAlign) {
    zr.add(new zrender.Text({
      style: {
        x: x,
        y: y,
        text: text,
        textFill: '#fff',
        textFont: textFont || '14px Microsoft Yahei',
        textAlign: textAlign || 'left'
      }
    }));
  },

  zrAddCircleImg(x, y, url, w, h, cx, cy, r) {
    let recomAvator = new zrender.Image({
      style: {
        x: x,
        y: y,
        image: url,
        width: w,
        height: h
      }
    });
    let circle = new zrender.Circle({
      shape: {
        cx: cx,
        cy: cy,
        r: r
      }
    });
    recomAvator.setClipPath(circle);
    zr.add(recomAvator);
  },

  zrAddLine(x1, y1, x2, y2) {
    zr.add(new zrender.Line({
      shape: {
        x1: x1,
        y1: y1,
        x2: x2,
        y2: y2
      },
      style: {
        lineWidth: 2,
        stroke: '#fff',
      }
    }));
  },

  renderCanvas() {
    zr = zrhelper.createZrender('canvas');
    // 上部背景图
    this.zrAddImage(0, 0, '../../../../resource/images/bg_invitation_card.png', this.data.width, this.data.height);
    // 推荐用户-头像
    this.zrAddCircleImg(20, 20, this.data.avatarTemp, 36, 36, 38, 38, 18);
    // 文案说明-顶部
    this.zrAddText(65, 34, this.data.nickName + '对你说：');

    // 小程序码
    this.zrAddImage(40, 380, this.data.miniCodeTemp, 60, 60);

    wx.hideLoading();
    // zr.on('rendered', () => {
    //   setTimeout(() => {
    //     wx.canvasToTempFilePath({
    //       canvasId: 'canvas',
    //       quality: 1,
    //       fileType: 'jpg',
    //       success: (res) => {
    //         this.setData({
    //           postImg: res.tempFilePath
    //         });
    //       }
    //     })
    //   }, 500);
    // })
  },

  onKeepPoster() {
    wx.showLoading({
      title: '保存中...',
      mask: true,
    })
    let timer = setTimeout(() => {
      clearTimeout(timer);
      wx.canvasToTempFilePath({
        canvasId: 'canvas',
        quality: 1,
        fileType: 'jpg',
        success: (res) => {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: () => {
              wx.hideLoading();
              wx.showToast({
                title: '已保存到相册',
              })
            },
            fail: (err) => {
              console.log(err);
              wx.hideLoading()
            }
          })
        },
        fail: () => {
          wx.hideLoading()
        }
      })
    }, 500);
  }
})